//===============================================================================
// Microsoft patterns & practices
// Web Client Software Factory
//-------------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//-------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================
using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WCSFContrib.PageFlow.Xml.Tests.Mocks;
using WCSFContrib.PageFlow.Xml.Storage.Session;
using WCSFContrib.PageFlow;

namespace WCSFContrib.PageFlow.Xml.Tests.Storage.Session
{
    [TestClass]
    public class XmlPageFlowInstanceCorrelationSessionProviderFixture
    {
        [TestMethod]
        public void CanCreateWithParameters()
        {
            XmlPageFlowInstanceCorrelationSessionProvider provider = new XmlPageFlowInstanceCorrelationSessionProvider(null, null);

            Assert.IsNotNull(provider);
        }
    
        [TestMethod]
        public void CanAddInstance()
        {
            TestableXmlPageFlowInstanceCorrelationSessionProvider provider = new TestableXmlPageFlowInstanceCorrelationSessionProvider();
            provider.Add(new MockPageFlow());
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void AddWithNullFails()
        {
            TestableXmlPageFlowInstanceCorrelationSessionProvider provider = new TestableXmlPageFlowInstanceCorrelationSessionProvider();
            provider.Add(null);
        }

        [TestMethod]
        public void InstanceIsAddedToSession()
        {
            IPageFlow pageFlow = new MockPageFlow();
            TestableXmlPageFlowInstanceCorrelationSessionProvider provider = new TestableXmlPageFlowInstanceCorrelationSessionProvider();
            provider.Add(pageFlow);
            Assert.IsTrue(provider.MockState.Contains(pageFlow.Id));
            Assert.AreEqual(pageFlow.Definition.PageFlowType.AssemblyQualifiedName, provider.MockState.GetInstanceType(pageFlow.Id));
        }

        [TestMethod]
        public void CanRemoveInstanceUsingPageFlowInstance()
        {
            IPageFlow pageFlow = new MockPageFlow();
            TestableXmlPageFlowInstanceCorrelationSessionProvider provider = new TestableXmlPageFlowInstanceCorrelationSessionProvider();
            provider.Add(pageFlow);
            provider.Remove(pageFlow);
            Assert.IsFalse(provider.MockState.Contains(pageFlow.Id));
        }

        [TestMethod]
        public void CanRemoveInstanceUsingInstanceId()
        {
            IPageFlow pageFlow = new MockPageFlow();
            TestableXmlPageFlowInstanceCorrelationSessionProvider provider = new TestableXmlPageFlowInstanceCorrelationSessionProvider();
            provider.Add(pageFlow);
            provider.Remove(pageFlow.Id);
            Assert.IsFalse(provider.MockState.Contains(pageFlow.Id));
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void RemoveWithNullFails()
        {
            TestableXmlPageFlowInstanceCorrelationSessionProvider provider = new TestableXmlPageFlowInstanceCorrelationSessionProvider();
            provider.Remove(null);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void RemoveWithEmptyIdFails()
        {
            TestableXmlPageFlowInstanceCorrelationSessionProvider provider = new TestableXmlPageFlowInstanceCorrelationSessionProvider();
            provider.Remove(Guid.Empty);
        }

        [TestMethod]
        public void GetLastRunningInstanceWhenAddingMorePageFlows()
        {
            TestableXmlPageFlowInstanceCorrelationSessionProvider provider = new TestableXmlPageFlowInstanceCorrelationSessionProvider();
            IPageFlow pageFlow1 = new MockPageFlow();
            IPageFlow pageFlow2 = new MockPageFlow();
            provider.Add(pageFlow1);
            provider.Add(pageFlow2);
            Assert.AreEqual(Guid.Empty, provider.GetLastRunningInstance());
            provider.SetPageFlowRunning(pageFlow1);
            Assert.AreEqual(pageFlow1.Id, provider.GetLastRunningInstance());
            provider.SetPageFlowRunning(pageFlow2);
            Assert.AreEqual(pageFlow2.Id, provider.GetLastRunningInstance());
        }

        [TestMethod]
        public void GetLastRunningInstanceWhenRemovedReturnsEmpty()
        {
            TestableXmlPageFlowInstanceCorrelationSessionProvider provider = new TestableXmlPageFlowInstanceCorrelationSessionProvider();
            IPageFlow pageFlow = new MockPageFlow();
            provider.Add(pageFlow);
            Assert.AreEqual(Guid.Empty, provider.GetLastRunningInstance());
            provider.SetPageFlowRunning(pageFlow);
            Assert.AreEqual(pageFlow.Id, provider.GetLastRunningInstance());
            provider.Remove(pageFlow);
            Assert.AreEqual(Guid.Empty, provider.GetLastRunningInstance());
        }

        [TestMethod]
        public void GetByTypeShouldReturnId()
        {
            TestableXmlPageFlowInstanceCorrelationSessionProvider provider = new TestableXmlPageFlowInstanceCorrelationSessionProvider();
            IPageFlow pageFlow = new MockPageFlow();
            provider.Add(pageFlow);
            Assert.AreEqual(pageFlow.Id, provider.GetByType(pageFlow.Definition.PageFlowType));
        }

        [TestMethod]
        public void GetByTypeShouldReturnEmptyGuidWhenNonExistantOrRemoved()
        {
            TestableXmlPageFlowInstanceCorrelationSessionProvider provider = new TestableXmlPageFlowInstanceCorrelationSessionProvider();
            Assert.AreEqual(Guid.Empty, provider.GetByType(typeof(MockUnconstrainedPageFlow)));
            IPageFlow pageFlow = new MockPageFlow();
            provider.Add(pageFlow);
            provider.Remove(pageFlow.Id);
            Assert.AreEqual(Guid.Empty, provider.GetByType(pageFlow.Definition.PageFlowType));
        }

        [TestMethod]
        public void GetInstanceTypeReturnsCorrectName()
        {
            TestableXmlPageFlowInstanceCorrelationSessionProvider provider = new TestableXmlPageFlowInstanceCorrelationSessionProvider();
            IPageFlow pageFlow = new MockPageFlow();
            provider.Add(pageFlow);
            Assert.AreEqual(pageFlow.Definition.PageFlowType.AssemblyQualifiedName, provider.GetInstanceType(pageFlow.Id));
        }
    }

    class TestableXmlPageFlowInstanceCorrelationSessionProvider : XmlPageFlowInstanceCorrelationSessionProvider
    {
        public PageFlowInstaceSessionTracker MockState = new PageFlowInstaceSessionTracker();
        protected override PageFlowInstaceSessionTracker State
        {
            get
            {
                return MockState;
            }
        }
    }

    
}
